﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <!--<a-form-model-item label="更新时间" prop="UpdateTime">-->
        <!--  <a-input v-model="entity.UpdateTime" autocomplete="off" placeholder="更新时间" />-->
        <!--</a-form-model-item>-->
        <a-form-model-item label="电梯载重" prop="E_Load">
          <a-input v-model="entity.E_Load" autocomplete="off" placeholder="电梯载重" @change="calcPeopleNum" suffix="kg"/>
        </a-form-model-item>
        <a-form-model-item label="人数" prop="PeopleNum">
          <a-input v-model="entity.PeopleNum" autocomplete="off" placeholder="人数" :disabled="true" suffix="人"/>
        </a-form-model-item>
        <a-form-model-item label="轿厢最小有效面积" prop="MinArea">
          <a-input v-model="entity.MinArea" autocomplete="off" placeholder="轿厢最小有效面积" suffix="㎡"/>
        </a-form-model-item>
        <a-form-model-item label="轿厢最大有效面积" prop="MaxArea">
          <a-input v-model="entity.MaxArea" autocomplete="off" placeholder="轿厢最大有效面积" suffix="㎡"/>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remark">
          <a-textarea v-model="entity.Remark" autocomplete="off" placeholder="备注" :row="5"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    const validateData = function (rule, value, callback) {
      if (rule.field === 'E_Load') {
        if (!value) {
          return callback(new Error('请输入载重'))
        }
        if (!/^\d+$/.test(value)) {
          return callback(new Error('请输入正确数字(非负整数)'))
        }
      }
      if (rule.field === 'MinArea') {
        if (!value) {
          return callback(new Error('请输入轿厢最小有效面积'))
        }
        if (!/^[0-9]+(.[0-9]{1,4})?$/.test(value)) {
          return callback(new Error('请输入正确数字(非负数字,最多包含4位小数)'))
        }
      }
      if (rule.field === 'MaxArea') {
        if (!value) {
          return callback(new Error('请输入轿厢最大有效面积'))
        }
        if (!/^[0-9]+(.[0-9]{1,4})?$/.test(value)) {
          return callback(new Error('请输入正确数字(非负数字,最多包含4位小数)'))
        }
      }
    }
    return {
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        E_Load: [{ validator: validateData, required: true, trigger: ['blur', 'change'] }],
        MinArea: [{ validator: validateData, required: true, trigger: ['blur', 'change'] }],
        MaxArea: [{ validator: validateData, required: true, trigger: ['blur', 'change'] }]
      },
      title: ''
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/Elevator_Manage/Elevator_LiftCarArea/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Elevator_Manage/Elevator_LiftCarArea/SaveData', this.entity).then(resJson => {
          this.loading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.loading = false
            this.visible = false
            this.$emit('returnMain')
          } else {
            this.$message.error(resJson.Msg)
            this.loading = false
          }
        })
      })
    },
    calcPeopleNum () {
      this.entity.PeopleNum = Math.trunc(this.entity.E_Load / 75)
    },
    // 非空校验
    validEmpty (index, name, tips) {
      return (rule, value, callback) => {
        const data = this.DetaiData[index][name]
        if (!data) {
          this.$message.warning('列表第' + (index + 1) + '行：' + tips)
          callback(tips || '必填')
        } else {
          callback()
        }
      }
    }
  }
}
</script>
<style scoped>
</style>
